/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.odg.dg.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.opu.odg.dg.GenerationSettings;
import org.opu.odg.dg.incomingdata.ContentDescription;
import org.opu.odg.dg.incomingdata.ContextIteration;
import org.opu.odg.dg.runtimedata.Content;
import org.opu.odg.dg.runtimedata.VariableHolder;
import org.opu.odg.dg.xml.TextMarkupUtil;
import org.opu.odg.dg.xml.TextMarkupUtil.Lexem;
import static org.opu.odg.dg.xml.TextMarkupUtil.LexemType.*;

/**
 *
 * @author yura
 */
class TextDocumentGenerator {
    
    private DocumentGenerationContext context;
    private GenerationSettings settings;
    private Map<String, ContentDescription> contents;
    private VariableHolder variableHolder;

    public TextDocumentGenerator(DocumentGenerationContext context,
            GenerationSettings settings, Map<String, ContentDescription> contents) {
        this.context = context;
        this.settings = settings;
        this.contents = contents;
        this.variableHolder = new VariableHolder(null, settings.getVariables());
    }

    String generateDocument(String template) throws IOException {
        return generateDocument(new ArrayList(TextMarkupUtil.split(template)), template.length() *3);
    }

    String generateDocument(List<Lexem> split, int optimalSize) throws IOException {
        StringBuffer sb = new StringBuffer(optimalSize);
        processLexemList(0, split, sb);
        return sb.toString();
    }

    private void processContent(List<Lexem> split, StringBuffer sb) throws IOException{
        String name = split.get(0).getText();
        Content content = new Content(contents.get(name), name,
                variableHolder.getContent(), context, variableHolder);
        variableHolder.setContent(content);

        for (ContextIteration iteration : content) {
            processLexemList(1, split, sb);
        }

        variableHolder.setContent(content.getParent());
        content.close();
    }

    private void processVar(Lexem lexem, StringBuffer sb){
//        sb.append(VariableResolver.assignVarValue(lexem.getText(), variableHolder));
        sb.append(variableHolder.getVarValue(lexem.getText()));
    }

    private void processLexemList(int start, List<Lexem> split, StringBuffer sb)
            throws IOException {
        for (int i = start; i < split.size(); i++) {
            Lexem lexem = split.get(i);
            switch (lexem.getType()) {
                case CONTENT_START:
                    processContent(split.subList(i, i = split.indexOf(
                            new Lexem(lexem.getText(), CONTENT_END))), sb);
                    break;
                case VARIABLE:
                    processVar(lexem, sb);
                    break;
                case TEXT:
                    try {
                        sb.append(lexem.getText());
                    } catch (Error e) {
                        e.printStackTrace(System.out);
                        throw e;
                    }
                    break;
                default:
                    break;
            }
        }
    }

}
